﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using System.Data.Linq;

namespace FlowersStore
{
	public partial class ProductDetails : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				if (Request.UrlReferrer != null)
					btnBack.NavigateUrl = Request.UrlReferrer.ToString();

				long productId = -1;
				if (Request.QueryString["product"] == null)
					Response.Redirect("Default.aspx");
				else if(!long.TryParse(Request.QueryString["product"].ToString(),out productId))
					Response.Redirect("Default.aspx");
				FlowerStoreDataContext db = new FlowerStoreDataContext();
				var product = (from p in db.Products
							   where p.Product_id == productId
							   select p).SingleOrDefault();
				if (product == null)
					Response.Redirect("Default.aspx");
				else
				{
                //    (FormView1.FindControl("Product_nameLabel") as Label).Text = product.Product_name;
                //    (FormView1.FindControl("Label_Product_article") as Label).Text = product.Product_article;
                ////    (FormView1.FindControl("ProductDesc") as Label).Text = product.Product_description;
                //    (FormView1.FindControl("Label_ProductPrice") as Label).Text = product.Product_price.ToString();
                //    (FormView1.FindControl("ProductDesc") as HtmlGenericControl).InnerText = product.Product_description;
                //    SqlDataSourceTogether.DataBind();

				}
				FillMainMenu();
			}
		//	((FlowersStore.SiteMaster)this.Master).FillCartItems();
		}

		private void FillMainMenu()
		{
			TreeViewMainMenu.Nodes.Clear();
			DataLoadOptions opts = new DataLoadOptions();
			opts.LoadWith<Department>(d => d.Categories);

			FlowerStoreDataContext db = new FlowerStoreDataContext();
			var depts = (from dep in db.Departments
						 select dep);
			
			foreach (Department d in depts)
			{
				TreeNode node = new TreeNode(d.Department_name.ToLower());
				//node.NavigateUrl = Request.Path;
				node.ImageUrl = "Styles/Images/ArrowMenu2.png";
				//depItem.Selectable = false;
				TreeViewMainMenu.Nodes.Add(node);
				foreach (Category cat in d.Categories)
				{
					TreeNode tn = new TreeNode("     " + cat.Category_name, cat.Category_id.ToString());
					tn.NavigateUrl = "Default.aspx" + "?category=" + cat.Category_id;
					node.ChildNodes.Add(tn);
				}
			}
			TreeViewMainMenu.CollapseAll();
		}

		protected void TreeViewMainMenu_SelectedNodeChanged(object sender, EventArgs e)
		{
			var treeView = sender as TreeView;
			var node = treeView.SelectedNode;
			if (node == null) return;
			if (node.Parent == null)
				switch (node.Expanded)
				{
					case true:
						node.Collapse();
						break;
					case false:
						node.Expand();
						break;
				}
			treeView.SelectedNode.Selected = false;
		}
        
        protected void btnInCart_Click(object sender, EventArgs e)
        {
            string str = ((ImageButton)sender).CommandArgument;

            string StrCount = (FormView1.FindControl("TextBoxNumber") as TextBox).Text;
            int count = Convert.ToInt32(StrCount);
            int ProdId = Convert.ToInt32(Request.QueryString["product"]);
			if (Session["cart"] == null)
				Session["cart"] = new UserCart();
            UserCart cart = (UserCart) Session["cart"];
            cart.AddPosition(ProdId, count);
            ((FlowersStore.SiteMaster) this.Master).FillCartItems();			
        }
	}
}